package main

import (
	"fmt"
	"gin_maker_chat/model"
	"gin_maker_chat/server/handler"
	"gin_maker_chat/server/utils"
	"io"
	"net"
)

type Processor struct {
	Conn net.Conn
}

// 根据消息的类型执行对应的函数
func (this *Processor) handlerByMsgType(msg *model.Message) (err error) {

	switch msg.Type {
	case model.ResisterMsgType:
		//处理注册信息
		userHandler := &handler.UserHandler{
			Conn: this.Conn,
		}
		err = userHandler.HandlerResisterMsg(msg)

	case model.LoginMsgType:
		//处理登录信息
		userHandler := &handler.UserHandler{
			Conn: this.Conn,
		}
		err = userHandler.HandlerLoginMsg(msg)
		// return
	default:
		fmt.Println("消息类型不存在，无法处理。")
	}
	return
}

func (this *Processor) process() (err error) {
	for {
		//读取客户端消息，封装成一个方法，返回Message
		transfer := &utils.Transfer{
			Conn: this.Conn,
		}
		msg, err := transfer.ReadMsgByNetCon()
		if err != nil {
			if err == io.EOF {
				fmt.Println("客户端退出，服务器端也退出..")
				return err
			} else {
				fmt.Println("read pkg err=", err)
				return err
			}

		}
		fmt.Println("msg=", msg)
		err = this.handlerByMsgType(&msg)
		if err != nil {
			fmt.Println("服务端异常err=", err)
			return err
		}

	}
}
